<?php
/**
 * @ Author: anchen
 * @ Date:   2016-04-29 18:27:31
 * @ Last Modified by:   anchen
 * @ Last Modified time: 2016-04-29 21:09:49
 * @ csv文件的游戏道具表导入转成数组格式
 */
//header("Content-type:text/html;charset=utf-8");
//打开csv文件
$handle = fopen("./xls/ItemTypeData20160905.csv","r");
//定义csv转成数组的函数
function input_csv($handle)
{
    $out = array ();
    $n = 0;
    while ($data = fgetcsv($handle, 10000))
    {
        //排除不需要的行(一般有两行,根据实际道具表更改)
        if($n==0||$n==1)
        {
            $n++;
            continue;
        }
        //获取需要的列
        foreach($data as $k=>$v){
            if($k==0){
                $i_itemID = $v;//物品ID: i_itemID
            }else if($k==3){
                $newdata['s_name'] = $v;//物品名称: s_name
            }else if($k==4){
                $newdata['i_goodstype'] = $v;//物品类型: i_goodstype
            }else if($k==8){
                $newdata['i_itemQuality'] = $v;//物品质量: i_itemQuality
            }else if($k==26){
                $newdata['i_wearJob'] = $v;//装备穿戴职业: i_wearJob
            }else if($k==7){
                $newdata['i_useLvReq'] = $v;//使用等级: i_useLvReq
            }else if($k==34){
                $newdata['i_itemPlace'] = $v;//装备部位: i_itemPlace
            }
        }
        //重新排列键值的顺序
        $newarray['s_name'] = $newdata['s_name'];
        $newarray['i_goodstype'] = $newdata['i_goodstype'];
        $newarray['i_itemQuality'] = $newdata['i_itemQuality'];
        $newarray['i_wearJob'] = $newdata['i_wearJob'];
        $newarray['i_useLvReq'] = $newdata['i_useLvReq'];
        $newarray['i_itemPlace'] = $newdata['i_itemPlace'];
        $out[$i_itemID] = $newarray;
    }
    //返回最终数组
    return $out;
}
//调用函数
$array = input_csv($handle);
//对"键"进行sort排序
ksort($array);
//以字符串方式组成需要的格式
$str = "&lt;?php<br>return array(<br/>";
foreach($array as $k=>$v){
    $str .= "'".$k."'=>array(";
    foreach($v as $vk=>$vv){
        $str .= "'".$vk."'=>'".$vv."',";
    }
    $str .= "),<br/>";
}
$str .= ");<br>?&gt;";
//输出字符串
echo $str;

/*
 * @ var_export()函数也可以输出类似array(1=>'a',2=>'b');的格式
 * @ 需要注意编码格式,最好不要和header("Content-type:text/html;charset=utf-8");同时使用
 */
//var_export($array);

?>